package org.darack_application;

import java.util.*;

import android.app.*;
import android.app.AlertDialog.Builder;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.*;

public class LoginDialog extends Activity implements Runnable {
	FirstActivity aActivity = (FirstActivity)FirstActivity.FirstActivity;
	ArrayList<String> login_iden_num, login_name, login_phone, login_email,	login_workarea;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		WindowManager.LayoutParams lpWindow = new WindowManager.LayoutParams();
		lpWindow.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
		lpWindow.dimAmount = 0.8f;
		getWindow().setAttributes(lpWindow);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.login_dialog);
		login_iden_num = new ArrayList<String>();
		login_name = new ArrayList<String>();
		login_phone = new ArrayList<String>();
		login_email = new ArrayList<String>();
		login_workarea = new ArrayList<String>();
		MThread mt = new MThread(login_iden_num, login_name, login_phone, login_email, login_workarea);
		mt.start();

		try {
			mt.join();
		} catch (Exception e) {

		}
		View.OnClickListener leftClickListener = new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				String inID = login_id.getText().toString();
				String inPW = login_num.getText().toString() + "기";

				int nameIndex = login_name.indexOf(inID);

				
				if (login_iden_num.contains(inPW) && login_name.contains(inID)) {
					if (login_iden_num.get(nameIndex).equals(inPW)) {
						CDialog.showLoading(LoginDialog.this);
						(new Thread(LoginDialog.this)).start();
					}
					else{
						Toast.makeText(getApplicationContext(), "기수 또는 이름을 잘못 입력하였습니다.", Toast.LENGTH_LONG).show();
					}
				} else if(!(login_iden_num.contains(inPW) && login_name.contains(inID))){
					Toast.makeText(getApplicationContext(), "기수 또는 이름을 잘못 입력하였습니다.", Toast.LENGTH_LONG).show();
				} else {
					Toast.makeText(getApplicationContext(), "기수 또는 이름을 잘못 입력하였습니다.", Toast.LENGTH_LONG).show();
				}
				
			}
		};

		View.OnClickListener rightClickListener = new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		};

		setLayout();
		setClickListener(leftClickListener, rightClickListener);
	}

	private void setClickListener(View.OnClickListener left,
			View.OnClickListener right) {
		if (left != null && right != null) {
			mLeftButton.setOnClickListener(left);
			mRightButton.setOnClickListener(right);
		} else if (left != null && right == null) {
			mLeftButton.setOnClickListener(left);
		} else {

		}
	}

	private EditText login_num;
	private EditText login_id;
	private Button mLeftButton;
	private Button mRightButton;
	
	private void setLayout() {
		login_id = (EditText) findViewById(R.id.login_name);
		login_num = (EditText) findViewById(R.id.login_num);
		mLeftButton = (Button) findViewById(R.id.bt_left);
		mRightButton = (Button) findViewById(R.id.bt_right);
	}

	@Override
	public void run() {
		try {
			Thread.sleep(3000);

		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		CDialog.hideLoading();

		Intent intent = new Intent(this, MainActivity.class);
		startActivity(intent);
		finish();
		aActivity.finish();
	}
	public void onBackPressed() {
		
		Builder d = new AlertDialog.Builder(this);
		d.setMessage("정말 종료하시겠습니까?");
		d.setPositiveButton("예", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				finish();
				aActivity.finish();
			}
		});
		d.setNegativeButton("아니요", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {

			}
		});
		d.show();
	} 

}
